MyServiceInterface.java
package com.ivoronline.springboot_autowired_primary.services;
public interface MyServiceInterface {
public String sayHello();
}
MyServiceImplementation1.java
package com.ivoronline.springboot_autowired_primary.services;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
@Service
@Primary
public class MyServiceImplementation1 implements MyServiceInterface {
public String sayHello() {
return "Hello";
}
}
MyServiceImplementation2.java
package com.ivoronline.springboot_autowired_primary.services;
import org.springframework.stereotype.Service;
@Service
public class MyServiceImplementation2 implements MyServiceInterface {
public String sayHello() {
return "Hello World";
}
}
MyController.java
package com.ivoronline.springboot_autowired_primary.controllers;
import com.ivoronline.springboot_autowired_primary.services.MyServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Autowired
MyServiceInterface myService;
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
String Results = myService.sayHello();
return Results;
}
}